/**

 */
package com.adkfp.utils;

import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

/**
 * class name:SelectclassAction <BR>
 * class description: please write your description <BR>
 * Remark: <BR>
 * @version 1.00 2019年1月15日
 * @author ADK)zhoulin
 */
public class ImageUtils {
	/**
	 * 上传图片
	 * @param request
	 * @param book
	 * @param pictureFile
	 * @throws IOException
	 */
	public static String upload(HttpServletRequest request,MultipartFile pictureFile) throws IOException {
		String imgPath = null;//装配后的图片地址
		//上传图片
		if(!pictureFile.isEmpty()){
			// 使用UUID给图片重命名，并去掉四个“-”
			String name = UUID.randomUUID().toString().replaceAll("-", "");
			// 获取文件的扩展名
			String ext = FilenameUtils.getExtension(pictureFile.getOriginalFilename());
			// 设置图片上传路径
			String url = request.getSession().getServletContext()
					.getRealPath("/upload");
			System.out.println(url);
			// 以绝对路径保存重名命后的图片
			pictureFile.transferTo(new File(url + "/" + name + "." + ext));
			// 装配图片地址
			imgPath = "upload/" + name + "." + ext;
		}
		return imgPath;
	}
	
	public static String uploadFile(MultipartFile file,String path) throws IOException {
		String name=file.getOriginalFilename();
		String suffixName=name.substring(name.lastIndexOf("."));
		String hash=Integer.toHexString(new Random().nextInt());
		String fileName=hash+suffixName;
		File tempFile=new File(path,fileName);
		if(!tempFile.getParentFile().exists()) {
			tempFile.getParentFile().mkdir();
		}
		if(tempFile.exists()) {
			tempFile.delete();
		}
		tempFile.createNewFile();
		file.transferTo(tempFile);
		return path;
	}
}
